//
//  ColorView.m
//  ShakeProject
//
//  Created by zkingsoft on 16/2/17.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//

#define COLOR_COUNT 4
#import "ColorView.h"

@interface ColorView (){
    UIView    *myColorView;
    
    NSArray   *buttonTitleArray;
}

@end

@implementation ColorView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        float singColorViewHeight = KHIGTH/COLOR_COUNT;
        buttonTitleArray = [[NSArray alloc] initWithObjects:@"HOME",@"HOSTORY",@"SETTING", @"OTHER", nil];
        
        for (int i = 0; i < COLOR_COUNT; i++) {
            myColorView = [[UIView alloc] initWithFrame:CGRectMake(-20, -KHIGTH, KWIDTH+60, KHIGTH)];
            myColorView.tag = 100+i;
            float redRandom = arc4random()%256;
            float greenRandom = arc4random()%256;
            float blueRandom = arc4random()%256;
            myColorView.backgroundColor = [UIColor colorWithRed:redRandom/255.0 green:greenRandom/255.0 blue:blueRandom/255.0 alpha:1.0];
            [self addSubview:myColorView];
            
            UIButton *myColorButton = [UIButton buttonWithType:UIButtonTypeCustom];
            myColorButton.tag = 1000+i;
            myColorButton.frame = CGRectMake(0, singColorViewHeight*(COLOR_COUNT-1), myColorView.frame.size.width, singColorViewHeight);
            [myColorButton setTitle:[buttonTitleArray objectAtIndex:i] forState:UIControlStateNormal];
            [myColorButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [myColorView addSubview:myColorButton];
            [myColorButton addTarget:self action:@selector(colorViewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    return self;
}

- (void)colorViewButtonClicked:(UIButton *)sender{
    UIView *animationColorView1 = [self viewWithTag:100];
    UIView *animationColorView2 = [self viewWithTag:101];
    UIView *animationColorView3 = [self viewWithTag:102];
    UIView *animationColorView4 = [self viewWithTag:103];
    
    CGRect frame = animationColorView1.frame;
    frame.origin.y -= KHIGTH+20;
    
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        animationColorView4.frame = frame;
        animationColorView3.frame = frame;
        animationColorView2.frame = frame;
        animationColorView1.frame = frame;
    } completion:^(BOOL finished) {
        NSString *titleString = sender.titleLabel.text;
        
        if ([_delegate respondsToSelector:@selector(receivedColorViewButtonClicked:)]) {
            [_delegate receivedColorViewButtonClicked:titleString];
        };
    }];
}

- (void)shakeViewAnimation{
    UIView *animationColorView1 = [self viewWithTag:100];
    UIView *animationColorView2 = [self viewWithTag:101];
    UIView *animationColorView3 = [self viewWithTag:102];
    UIView *animationColorView4 = [self viewWithTag:103];
    
    
    
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        // 第一个视图做动画
        CGRect frame = animationColorView1.frame;
        frame.origin.y += KHIGTH+20;
        animationColorView1.frame = frame;
    } completion:^(BOOL finished) {
        
        // 结束之后第二个做动画 第一个左摆
        [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            CGRect frame = animationColorView2.frame;
            frame.origin.y += KHIGTH/4*3;
            animationColorView2.frame = frame;
            
            animationColorView1.transform = CGAffineTransformMakeRotation(M_PI/180*2);
        } completion:^(BOOL finished) {
            
            // 第三个做动画 第一个右摆 第二个左摆
            [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                CGRect frame = animationColorView3.frame;
                frame.origin.y += KHIGTH/4*2;
                animationColorView3.frame = frame;
                
                animationColorView1.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                animationColorView2.transform = CGAffineTransformMakeRotation(M_PI/180*2);
            } completion:^(BOOL finished) {
                
                // 第四个做动画
                [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    
                    CGRect frame = animationColorView4.frame;
                    frame.origin.y += KHIGTH/4;
                    animationColorView4.frame = frame;
                    
                    animationColorView1.transform = CGAffineTransformMakeRotation(0);
                    animationColorView2.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                    animationColorView3.transform = CGAffineTransformMakeRotation(M_PI/180*2);
                } completion:^(BOOL finished) {
                    
                    [UIView animateWithDuration:0.4 animations:^{
                        animationColorView2.transform = CGAffineTransformMakeRotation(0);
                        animationColorView3.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                        animationColorView4.transform = CGAffineTransformMakeRotation(M_PI/180*2);
                    } completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.4 animations:^{
                            animationColorView3.transform = CGAffineTransformMakeRotation(0);
                            animationColorView4.transform = CGAffineTransformMakeRotation(-M_PI/180*2);
                        } completion:^(BOOL finished) {
                            [UIView animateWithDuration:0.4 animations:^{
                                animationColorView4.transform = CGAffineTransformMakeRotation(0);
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
